﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace NesEmulatorX.UI
{
	public partial class frmMain : Form
	{
		#region Properties

		public NESROM ROM
		{
			get { return _rom; }
			set { _rom = value; }
		}
		private NESROM _rom = new NESROM();

		#endregion

		#region Public Methods

		public frmMain()
		{
			InitializeComponent();
		}

		#endregion

		#region Private Methods

		private void menuMain_File_Exit_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void menuMain_Help_About_Click(object sender, EventArgs e)
		{
			frmAbout f = new frmAbout();
			f.ShowDialog(this);
		}

		private void menuMain_File_OpenROM_Click(object sender, EventArgs e)
		{
			if (this.ofdNES.ShowDialog() != System.Windows.Forms.DialogResult.OK)
				return;

			var stream = this.ofdNES.OpenFile();

			BinaryReader reader = new BinaryReader(stream);
			var bytes = reader.ReadBytes((int)stream.Length);

			reader.Close();
			stream.Close();

			if (!this.ROM.Load(bytes))
				MessageBox.Show(String.Format("Failed to open ROM: '{0}'",
					this.ofdNES.FileName));

			this.EnableDebugging();
		}

		private void menuMain_Debug_OpenDebugger_Click(object sender, EventArgs e)
		{
			frmDebug f = new frmDebug();

			f.LoadROM(this.ROM);

			f.Show(this);
		}

		private void EnableDebugging()
		{
			this.menuMain_Debug_OpenDebugger.Enabled = true;
		}

		#endregion
	}
}
